package com.lemner.motion.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.view.View;

import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.lemner.motion.R;
import com.lemner.motion.activity.FindWayActivity;

import com.lemner.motion.base.BaseFragment;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link IndexFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class IndexFragment extends BaseFragment implements View.OnClickListener {
    private Button btn_startrun;
    private OnFragmentInteractionListener mListener;
    private TextView tv_time,tv_totalMileage,tv_totalTime;
    private LocationManager lm;//【位置管理】
    private String schoolname,studentname;

    public IndexFragment() {
        // Required empty public constructor
    }


    @Override
    public int getLayoutFile() {
        return R.layout.fragment_index;
    }

    @Override
    public void initView() {
        btn_startrun=(Button)view.findViewById(R.id.btn_startrun);
        tv_time=(TextView)view.findViewById(R.id.tv_time);
        tv_totalMileage=(TextView)view.findViewById(R.id.totalMileage);
        tv_totalTime=(TextView)view.findViewById(R.id.totalTime);
        getTime();
    }

   public void getTime(){
       @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日");
       Date curDate =  new Date(System.currentTimeMillis());
       String   str   =   formatter.format(curDate);
       tv_time.setText(str);
   }

    @Override
    public void initEvent() {
        btn_startrun.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initData() {
        if((getActivity().getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
            getActivity().finish();
            return;
        }
        int totalMileage =getArguments().getInt("totalMileage");
        schoolname=getArguments().getString("school");
        studentname=getArguments().getString("studentname");
        int totalTime=getArguments().getInt("totalTime");
        if(totalMileage>0)
            tv_totalMileage.setText(totalMileage);
        if(totalTime>0)
            tv_totalTime.setText(totalTime/60+"");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_startrun:
                lm=(LocationManager)getActivity().getSystemService(getActivity().LOCATION_SERVICE);
                boolean ok=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
                if(ok) {
                    Intent intent=new Intent(getActivity(),FindWayActivity.class);
                    intent.putExtra("shcoolname",schoolname);
                    intent.putExtra("studentname",studentname);
                    startActivity(intent);
                }
                    else{
                    Toast.makeText(getActivity(), "系统检测到未开启GPS定位服务", Toast.LENGTH_LONG).show();
                    Intent intent=new Intent();
                    intent.setAction(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }

                break;
        }
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
